﻿using UnityEngine;
using System.Collections;
using Modding;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using System.IO;
using System.Linq;
using System;

public class Controller : SingleInstance<Controller>
{
    public string FilePath = Application.dataPath + "/Mods/MachineToOBJMod";
    public string FileName = "mesh";
    public override string Name { get; } = "Controller";
    public bool showGUI = true;

    private readonly int WindowID = ModUtility.GetWindowId();
    private Rect windowRect = new Rect(Screen.width * 0.05f, Screen.height * 0.5f, 200, 140);

    public Action<string,string> OnExportMachine;
    public Action<string,string> OnExportLevel;
    public Action OnDelete;
    public Action OnOpenDirectory;

    public float Percent { set; get; } = 0;

    //private ObjExporterScript objExporter;

    void Awake()
    {
        //objExporter = GetComponent<ObjExporterScript>();
    }

    void Update() { if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.F7)) showGUI = !showGUI; }

    void OnGUI() { if (showGUI && !StatMaster.levelSimulating && IsBuilding() && !StatMaster.inMenu) { windowRect = GUI.Window(WindowID, windowRect, new GUI.WindowFunction(SceneWindow), "存档导出为OBJ  Ctrl+F7"); } }

    void SceneWindow(int ID)
    {

        GUILayout.BeginVertical();
        {
            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            {
                GUILayout.Label("文件名:");
                FileName = GUILayout.TextField(FileName, GUILayout.MinWidth(115f));
            }
            GUILayout.EndHorizontal();
            GUILayout.Space(5);

            GUILayout.HorizontalScrollbar(0f,/*objExporter.percent*/Percent, 0.00f, 1.00f);

            GUILayout.Space(5);
            GUILayout.BeginHorizontal();
            {
                GUILayout.BeginVertical();
                {
                    if (GUILayout.Button("导出存档")) { OnExportMachineClick(); }
                    if (GUILayout.Button("删除网格")) { OnDeleteClick(); }
                }
                GUILayout.EndVertical();
                GUILayout.Space(5);
                GUILayout.BeginVertical();
                {
                    if (GUILayout.Button("导出地形")) { OnExportLevelClick(); }
                    if (GUILayout.Button("打开文件夹")) { OnOpenDirectoryClick(); }
                }
                GUILayout.EndVertical();
            }
            GUILayout.EndHorizontal();
        }
        GUILayout.EndVertical();

        GUI.DragWindow();
    }

    public void OnExportMachineClick() { OnExportMachine?.Invoke(FilePath,FileName); /*objExporter.ExportMachineToOBJ(FilePath,FileName);*/ }

    public void OnExportLevelClick() { OnExportLevel?.Invoke(FilePath,FileName); /*objExporter.ExportLevelToOBJ(FilePath, FileName);*/ }

    public void OnDeleteClick() { OnDelete?.Invoke(); /*objExporter.ClearMesh();*/ }

    public void OnOpenDirectoryClick()
    {
        if (!Directory.Exists(FilePath)) { Directory.CreateDirectory(FilePath); }

        System.Diagnostics.Process.Start(@FilePath);

        OnOpenDirectory?.Invoke();
    }

    private bool IsBuilding()
    {
        List<string> scene = new List<string> { "INITIALISER", "TITLE SCREEN", "LevelSelect", "LevelSelect1", "LevelSelect2", "LevelSelect3" };

        if (SceneManager.GetActiveScene().isLoaded)
        {

            if (!scene.Exists(match => match == SceneManager.GetActiveScene().name))
            {
                return true;
            }
            return false;
        }

        return false;

    }

}
